package wenjun.du.androidart.viewcreate

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import wenjun.du.androidart.R


/**
 * 自定义子控件 一般会重写onMeasure 和 onDraw方法
 *
 * 自定义范例：
 *
 * */
@SuppressLint("CustomViewStyleable")
class ExView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
    View(context, attrs, defStyleAttr) {

    var mColor: Int = Color.RED
    private val mPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        val obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.CircleView)
        mColor = obtainStyledAttributes.getColor(R.styleable.CircleView_circle_color, Color.BLUE)
        mPaint.color = mColor
        obtainStyledAttributes.recycle()
    }

    companion object {
        private const val TAG = "ExView"
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(
        context,
        attrs,
        android.R.attr.textViewStyle
    )


    /**
     * measure
     *
     * 测绘出自己大小 并且设值测绘后控件的长、宽值
     *
     * 测绘结束后 通过方法setMeasuredDimension设值测绘后的宽、高值
     *
     * */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        //简单处理控件宽高设值成wrap_content的方式
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(200, 200)
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(200, heightSize)
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSize, 200)
        }

        Log.d(
            TAG,
            "onMeasure() called with: widthMeasureSpec = $widthMeasureSpec, heightMeasureSpec = $heightMeasureSpec"
        )
    }

    /**
     * layout:设值自己在父控件中的位置
     * 如果是子控件 onLayout方法作用不大 如果是父控件
     * 则遍历子控件 依次布局子控件在父控件中的位置
     * */
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        Log.d(
            TAG,
            "onLayout() called with: changed = $changed, left = $left, top = $top, right = $right, bottom = $bottom"
        )
    }

    /**
     * 画出控件本身的内容
     * */
    override fun onDraw(canvas: Canvas?) {
        val paddingLeft = paddingLeft
        val paddingRight = paddingRight
        val paddingTop = paddingTop
        val paddingBottom = paddingBottom
        val width = width - paddingLeft - paddingRight
        val height = height - paddingTop - paddingBottom
        val radius = width.coerceAtMost(height)
        canvas?.drawCircle(
            (paddingLeft + width / 2).toFloat(),
            (paddingTop + height / 2).toFloat(), (radius / 2).toFloat(), mPaint
        )
        Log.d(TAG, "onDraw() called with: canvas = $canvas")
    }
}